{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Comparisons - 🐭" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We need to be able to compare different variables. We will be working on:\n", "* Are these things the same?\n", "* Are these things not the same?\n", "* How do these things compare?\n", "\n", "We can compare any data type, and our output will be a boolean (True or False). The other things we will cover are:\n", "* Comparing different data types\n", "* Making multiple comparisons at once\n", "\n", "Comparison operators are important on their own (how do these things compare?) and are also useful for sorting and switching (see the next notebook)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Are these things the same?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### Numeric Comparisons\n", "We have already initiated variables by setting something equal to something else - let's do that here by setting kitten 🐈 equal to 10 and then setting dog 🐕 equal to kitten 🐈. Finally, 🐝 bee will be equal to 11. \n", "\n", "So...\n", "\n", "🐈 = 10\n", "\n", "🐕 = 🐈\n", "\n", "🐝 = 11" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "kitten = 10 ; dog = 10 ; bee = 11\n" ] } ], "source": [ "kitten = 10\n", "dog = kitten\n", "bee = 11 \n", "\n", "print( \"kitten =\", kitten, \"; dog =\", dog, \"; bee = \", bee )" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "The first comparison operator is '==', which tests to see if two variables are equal. " ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "kitten = 10 ; dog = 10 ; bee = 11\n", "Is kitten equal to dog?\n", "True\n", "Is kitten equal to bee?\n", "False\n" ] } ], "source": [ "print( \"kitten =\", kitten, \"; dog =\", dog, \"; bee = \", bee )\n", "\n", "print( \"Is kitten equal to dog?\")\n", "print( kitten == dog )\n", "\n", "print( \"Is kitten equal to bee?\")\n", "print( kitten == bee )" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "This tells us that kitten is equal to dog, because it returns *True* and kitten is not equal to bee, as that returns *False*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Character Comparisons\n", "We can also do comparisons with other variable types. Here's an example with strings instead of integers.\n", "\n", "Let's think about some foods, how about:\n", "\n", "- food1 = 🍎\n", "- food2 = 🍪\n", "- food3 = 🍎" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "food1= apple ; food2 = cookie ; food3 = apple\n", "Is food1 equal to food2?\n", "False\n", "Is food1 equal to food3?\n", "True\n" ] } ], "source": [ "food1 = 'apple'\n", "food2 = 'cookie'\n", "food3 = 'apple' \n", "print( \"food1=\", food1,\"; food2 =\", food2,\"; food3 = \", food3 )\n", "\n", "print( \"Is food1 equal to food2?\")\n", "print( food1 == food2 )\n", "\n", "print( \"Is food1 equal to food3?\")\n", "print( food1 == food3 )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Are these things different?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### This is Logical... NOT!\n", "We can also test to see if two values are not equal using the '!=' operator." ] }, { "cell_type": "code", "execution_count": 174, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "food1 = apple ; food2 = cookie ; food3 = apple\n", "Is food1 not equal to food2?\n", "True\n", "Is food1 not equal to food3?\n", "False\n" ] } ], "source": [ "print( \"food1 =\", food1,\"; food2 =\", food2,\"; food3 =\", food3 )\n", "\n", "print( \"Is food1 not equal to food2?\")\n", "print( food1 != food2 )\n", "\n", "print( \"Is food1 not equal to food3?\")\n", "print( food1 != food3 )" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "This gives us the opposite of what we had before. \n", "\n", "So, what did we learn?\n", "\n", "🍎 == 🍎 = *True*\n", "\n", "🍎 != 🍪 = *True*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How do these things compare?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Math Comparisons 101\n", "We can also compare the magnitude of values using '<', '<=', '>'and '>=', which will return 'True' if the condition is being met." ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "kitten = 10 ; dog = 10 ; bee = 11\n" ] } ], "source": [ "print( \"kitten =\", kitten, \"; dog =\", dog, \"; bee = \", bee )" ] }, { "cell_type": "code", "execution_count": 164, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Is kitten less than dog?\n", "False\n", "Is kitten less than or equal to dog?\n", "True\n", "Is kitten greater than or equal to dog?\n", "True\n", "Is kitten greater than dog?\n", "False\n" ] } ], "source": [ "print( \"Is kitten less than dog?\")\n", "print( kitten < dog )\n", "\n", "print( \"Is kitten less than or equal to dog?\")\n", "print( kitten <= dog )\n", "\n", "print( \"Is kitten greater than or equal to dog?\")\n", "print( kitten >= dog )\n", "\n", "print( \"Is kitten greater than dog?\")\n", "print( kitten > dog )" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "```{note}\n", "We do have to watch out for our types. Characters and numerics are **not** the same.\n", "```\n" ] }, { "cell_type": "code", "execution_count": 167, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Is TheNumbers equal to TheCharacters?\n", "False\n", "TheNumbers type is ; and TheCharacters type is \n" ] } ], "source": [ "TheCharacters = \"10\"\n", "TheNumbers = 10\n", "\n", "print( \"Is TheNumbers equal to TheCharacters?\")\n", "print( TheNumbers == TheCharacters )\n", "print( \"TheNumbers type is \", type( TheNumbers ), \"; and TheCharacters type is \", type( TheCharacters ) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can compare integers and floats (!) but not other disparate data types.\n", "\n", "If you let python take care of your data-types, be warned that they could be different from what you think they are!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Toy (Car) Problem\n", "\n", "To start thinking of these concepts from a logical perspective, let's create a toy (car) problem. Here are a bunch of toy cars with various colors and costs. Here is how they labeled.\n", "\n", "auto:\n", "- car\n", "- truck\n", "\n", "color:\n", "- red\n", "- blue\n", "- yellow\n", "- white\n", "- black\n", "\n", "cost:\n", "- 1 <= cost <= 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "varible = varible is **not** the same thing as variable == variable\n", "\n", "varible = varible will **always** return true\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multiple Comparisons" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can make multiple comparisons at once by stringing the statements\n", "* and\n", "* not\n", "* or\n", "\n", "together. \n", "\n", "The individual testable (true/false) components need to be broken apart. For example,\n", "* If the *V* CATA bus is coming around the corner, then I need to run towards the bus stop.\n", "\n", "requires several things for it to be true and to require running. We can break these things out with:\n", "* If there is a vehicle coming around the corner **AND** that vehicle is a CATA bus **AND** that CATA bus is a V \n", " * then I need to run towards the bus stop\n", "\n", "We will only run towards the bus stop if all of the statements are true." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### AND" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "```{note}\n", "the **and** operator will return True if all of the conditions are met\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's create another scenario for this around clothes. For this, let's assume:\n", "\n", "face = 😎 \n", "\n", "shirt = 👕\n", "\n", "pants = 👖 \n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 179, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Am I wearing sunglasses and jeans?\n", "True\n", "True\n", "True\n", "Am I wearing sweater and jeans?\n", "False\n", "True\n", "False\n" ] } ], "source": [ "face = \"sunglasses\"\n", "shirt = \"tshirt\"\n", "pants = \"jeans\"\n", "\n", "print ( \"Am I wearing sunglasses and jeans?\" )\n", "print (face == \"sunglasses\")\n", "print (pants == \"jeans\") \n", "print( (face == \"sunglasses\") and (pants == \"jeans\") )\n", "\n", "print ( \"Am I wearing sweater and jeans?\" )\n", "print (shirt == \"sweater\")\n", "print (pants == \"jeans\") \n", "print( (shirt == \"sweater\") and (pants == \"jeans\") )\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also string as many comparisons together as we want." ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "True" ] }, "execution_count": 180, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print( (1 < 2) and (1 < 3) and (1 < 4) and (1 < 5) and (1 < 6) and (1 < 7) and (1 < 8) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### OR" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "```{note}\n", "the **or** operator will return True if at least *1* of the conditions is met\n", "```" ] }, { "cell_type": "code", "execution_count": 184, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "face = sunglasses ; shirt = tshirt ; pants = jeans\n", "Am I wearing sunglasses or jeans?\n", "True\n", "True\n", "True\n", "Am I wearing sweater or jeans?\n", "False\n", "True\n", "True\n" ] } ], "source": [ "print( \"face =\", face, \"; shirt =\", shirt, \"; pants = \", pants )\n", "\n", "print ( \"Am I wearing sunglasses or jeans?\" )\n", "print (face == \"sunglasses\")\n", "print (pants == \"jeans\") \n", "print( (face == \"sunglasses\") or (pants == \"jeans\") )\n", "\n", "print ( \"Am I wearing sweater or jeans?\" )\n", "print (shirt == \"sweater\")\n", "print (pants == \"jeans\") \n", "print( (shirt == \"sweater\") or (pants == \"jeans\") )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Not" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{note}\n", "the **not** will reverse or switch the meaning of the and/or operators\n", "```" ] }, { "cell_type": "code", "execution_count": 187, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "face = sunglasses ; shirt = tshirt ; pants = jeans\n", "Am I wearing sunglasses and not jeans?\n", "True\n", "False\n", "False\n", "Am I wearing jeans and not a sweater?\n", "True\n", "True\n", "True\n" ] } ], "source": [ "print( \"face =\", face, \"; shirt =\", shirt, \"; pants = \", pants )\n", "\n", "print ( \"Am I wearing sunglasses and not jeans?\" )\n", "print (face == \"sunglasses\")\n", "print (not (pants == \"jeans\"))\n", "print( (face == \"sunglasses\") and not (pants == \"jeans\") )\n", "\n", "print ( \"Am I wearing jeans and not a sweater?\" )\n", "print (not (shirt == \"sweater\"))\n", "print (pants == \"jeans\") \n", "print( not (shirt == \"sweater\") and (pants == \"jeans\") )\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Try It Out!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try to fill in code to fulfill the request! Here are some variables used in the exercise" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "dogA_color = 'brown'\n", "dogA_mass = 42\n", "dogA_sex = 'male'\n", "dogA_age = 5\n", "dogA_name = 'chip'\n", "\n", "dogB_color = 'white'\n", "dogB_mass = 19\n", "dogB_sex = 'female'\n", "dogB_age = 2\n", "dogB_name = 'lady'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Is dogA the same color as dogB? (False)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "# Example:\n", "print( dogA_color == dogB_color )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Does dogA have the same name as dogB? (False)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "# Try it out here:\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Is dogA older than dogB? (True)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "# Try it out here:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Is dogA the same sex as dogB? (False)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "# Try it out here:\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Is dogA heavier than dogB and have a different name than dogB? (True)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "# Try it out here:\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Does dogA have a different age than dogB and not a different sex than dogB? (False)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "# Try it out here:\n" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 4 }